# RT-Thread building script for component
import rtconfig
Import('RTT_ROOT')
from building import *

cwd = GetCurrentDir()
src = Split('''
Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c
''')
CPPPATH = [cwd + '/Device/ST/STM32F4xx/Include', cwd + '/Include']

if GetDepend(['SOC_STM32F405RG']) or GetDepend(['SOC_STM32F405VG']) or GetDepend(['SOC_STM32F405ZG']):
    STM32_TYPE = 'STM32F405xx'
if GetDepend(['SOC_STM32F415RG']) or GetDepend(['SOC_STM32F415VG']) or GetDepend(['SOC_STM32F415ZG']):
    STM32_TYPE = 'STM32F415xx'
if GetDepend(['SOC_STM32F407VG']) or GetDepend(['SOC_STM32F407VE']) or GetDepend(['SOC_STM32F407ZG']) or GetDepend(['SOC_STM32F407ZE']) or GetDepend(['SOC_STM32F407IG']) or GetDepend(['SOC_STM32F407IE']):
    STM32_TYPE = 'STM32F407xx'
if GetDepend(['SOC_STM32F417VG']) or GetDepend(['SOC_STM32F417VE']) or GetDepend(['SOC_STM32F417ZG']) or GetDepend(['SOC_STM32F417ZE']) or GetDepend(['SOC_STM32F417IG']) or GetDepend(['SOC_STM32F417IE']):
    STM32_TYPE = 'STM32F417xx'
if GetDepend(['SOC_STM32F427VG']) or GetDepend(['SOC_STM32F427VI']) or GetDepend(['SOC_STM32F427ZG']) or GetDepend(['SOC_STM32F427ZI']) or GetDepend(['SOC_STM32F427IG']) or GetDepend(['SOC_STM32F427II']):
    STM32_TYPE = 'STM32F427xx'
if GetDepend(['SOC_STM32F437VG']) or GetDepend(['SOC_STM32F437VI']) or GetDepend(['SOC_STM32F437ZG']) or GetDepend(['SOC_STM32F437ZI']) or GetDepend(['SOC_STM32F437IG']) or GetDepend(['SOC_STM32F437II']):
    STM32_TYPE = 'STM32F437xx'
if GetDepend(['SOC_STM32F429VG']) or GetDepend(['SOC_STM32F429VI']) or GetDepend(['SOC_STM32F429ZG']) or GetDepend(['SOC_STM32F429ZI']) or GetDepend(['SOC_STM32F429BG']) or GetDepend(['SOC_STM32F429BI']) or GetDepend(['SOC_STM32F429NG']) or GetDepend(['SOC_STM32F429NI']) or GetDepend(['SOC_STM32F429IG']) or GetDepend(['SOC_STM32F429II']):
    STM32_TYPE = 'STM32F429xx'
if GetDepend(['SOC_STM32F439VG']) or GetDepend(['SOC_STM32F439VI']) or GetDepend(['SOC_STM32F439ZG']) or GetDepend(['SOC_STM32F439ZI']) or GetDepend(['SOC_STM32F439BG']) or GetDepend(['SOC_STM32F439BI']) or GetDepend(['SOC_STM32F439NG']) or GetDepend(['SOC_STM32F439NI']) or GetDepend(['SOC_STM32F439IG']) or GetDepend(['SOC_STM32F439II']):
    STM32_TYPE = 'STM32F439xx'
if GetDepend(['SOC_STM32F401CB']) or GetDepend(['SOC_STM32F401CC']) or GetDepend(['SOC_STM32F401RB']) or GetDepend(['SOC_STM32F401RC']) or GetDepend(['SOC_STM32F401VB']) or GetDepend(['SOC_STM32F401VC']):
    STM32_TYPE = 'STM32F401xC'
if GetDepend(['SOC_STM32F401CD']) or GetDepend(['SOC_STM32F401RD']) or GetDepend(['SOC_STM32F401VD']) or GetDepend(['SOC_STM32F401CE']) or GetDepend(['SOC_STM32F401RE']) or GetDepend(['SOC_STM32F401VE']):
    STM32_TYPE = 'STM32F401xE'
if GetDepend(['SOC_STM32F410T8']) or GetDepend(['SOC_STM32F410TB']):
    STM32_TYPE = 'STM32F410Tx'
if GetDepend(['SOC_STM32F410C8']) or GetDepend(['SOC_STM32F410CB']):
    STM32_TYPE = 'STM32F410Cx'
if GetDepend(['SOC_STM32F410R8']) or GetDepend(['SOC_STM32F410RB']):
    STM32_TYPE = 'STM32F410Rx'
if GetDepend(['SOC_STM32F411CC']) or GetDepend(['SOC_STM32F411RC']) or GetDepend(['SOC_STM32F411VC']) or GetDepend(['SOC_STM32F411CE']) or GetDepend(['SOC_STM32F411RE']) or GetDepend(['SOC_STM32F411VE']):
    STM32_TYPE = 'STM32F411xE'
if GetDepend(['SOC_STM32F446MC']) or GetDepend(['SOC_STM32F446ME']) or GetDepend(['SOC_STM32F446RC']) or GetDepend(['SOC_STM32F446RE']) or GetDepend(['SOC_STM32F446VC']) or GetDepend(['SOC_STM32F446VE']) or GetDepend(['SOC_STM32F446ZC']) or GetDepend(['SOC_STM32F446ZE']):
    STM32_TYPE = 'STM32F446xx'
if GetDepend(['SOC_STM32F469AI']) or GetDepend(['SOC_STM32F469II']) or GetDepend(['SOC_STM32F469BI']) or GetDepend(['SOC_STM32F469NI']) or GetDepend(['SOC_STM32F469AG']) or GetDepend(['SOC_STM32F469IG']) or GetDepend(['SOC_STM32F469BG']) or GetDepend(['SOC_STM32F469NG']) or GetDepend(['SOC_STM32F469AE']) or GetDepend(['SOC_STM32F469IE']) or GetDepend(['SOC_STM32F469BE']) or GetDepend(['SOC_STM32F469NE']):
    STM32_TYPE = 'STM32F469xx'
if GetDepend(['SOC_STM32F479AI']) or GetDepend(['SOC_STM32F479II']) or GetDepend(['SOC_STM32F479BI']) or GetDepend(['SOC_STM32F479NI']) or GetDepend(['SOC_STM32F479AG']) or GetDepend(['SOC_STM32F479IG']) or GetDepend(['SOC_STM32F479BG']) or GetDepend(['SOC_STM32F479NG']):
    STM32_TYPE = 'STM32F479xx'
if GetDepend(['SOC_STM32F412CEU']) or GetDepend(['SOC_STM32F412CGU']):
    STM32_TYPE = 'STM32F412Cx'
if GetDepend(['SOC_STM32F412ZET']) or GetDepend(['SOC_STM32F412ZGT']) or GetDepend(['SOC_STM32F412ZEJ']) or GetDepend(['SOC_STM32F412ZGJ']):
    STM32_TYPE = 'STM32F412Zx'
if GetDepend(['SOC_STM32F412VET']) or GetDepend(['SOC_STM32F412VGT']) or GetDepend(['SOC_STM32F412VEH']) or GetDepend(['SOC_STM32F412VGH']):
    STM32_TYPE = 'STM32F412Vx'
if GetDepend(['SOC_STM32F412RET']) or GetDepend(['SOC_STM32F412RGT']) or GetDepend(['SOC_STM32F412REY']) or GetDepend(['SOC_STM32F412RGY']):
    STM32_TYPE = 'STM32F412Rx'
if GetDepend(['SOC_STM32F413CH']) or GetDepend(['SOC_STM32F413MH']) or GetDepend(['SOC_STM32F413RH']) or GetDepend(['SOC_STM32F413VH']) or GetDepend(['SOC_STM32F413ZH']) or GetDepend(['SOC_STM32F413CG']) or GetDepend(['SOC_STM32F413MG']) or GetDepend(['SOC_STM32F413RG']) or GetDepend(['SOC_STM32F413VG']) or GetDepend(['SOC_STM32F413ZG']):
    STM32_TYPE = 'STM32F413xx'
if GetDepend(['SOC_STM32F423CH']) or GetDepend(['SOC_STM32F423RH']) or GetDepend(['SOC_STM32F423VH']) or GetDepend(['SOC_STM32F423ZH']):
    STM32_TYPE = 'STM32F423xx'
    
CPPDEFINES = [STM32_TYPE]

# add for startup script 
if rtconfig.CROSS_TOOL == 'gcc':
     folder = 'gcc'
     src += ['Device/ST/STM32F4xx/Source/Templates/gcc/gcc_startup.s']
elif rtconfig.CROSS_TOOL == 'keil':
     folder = 'arm'
elif rtconfig.CROSS_TOOL == 'iar':
     folder = 'iar'

#Device/ST/STM32F4xx/Source/Templates/iar/startup_stm32f411xe.s     
src += ['Device/ST/STM32F4xx/Source/Templates/' + folder + '/startup_' + STM32_TYPE.lower() + '.s']

group = DefineGroup('CMSIS', src, depend = [''], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)

Return('group')
